cmake_minimum_required(VERSION 3.20)
project(SM4SqliteCipher)

set(CMAKE_CXX_STANDARD 14)

if (NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
    message(FATAL_ERROR "Compile for the Linux platform !")
endif ()


if (TARGET_PLATFORM STREQUAL "win")
    find_program(C_COMPILER x86_64-w64-mingw32-gcc)
    find_program(CXX_COMPILER x86_64-w64-mingw32-g++)

    if (C_COMPILER AND CXX_COMPILER)
        message(STATUS "Found c_compiler at ${C_COMPILER}, Found cxx_compiler at ${CXX_COMPILER}")
    else ()
        message(FATAL_ERROR "compiler not found.")
    endif ()

    set(CMAKE_C_COMPILER "x86_64-w64-mingw32-gcc")
    set(CMAKE_CXX_COMPILER "x86_64-w64-mingw32-g++")
elseif (TARGET_PLATFORM STREQUAL "linux")
    find_program(C_COMPILER_1 x86_64-linux-gnu-gcc)
    find_program(CXX_COMPILER_1 x86_64-linux-gnu-g++)
    find_program(C_COMPILER_2 gcc)
    find_program(CXX_COMPILER_2 g++)
    find_program(C_COMPILER_3 cc)
    find_program(CXX_COMPILER_3 c++)

    if (C_COMPILER_1 AND CXX_COMPILER_1)
        message(STATUS "Found c_compiler at ${C_COMPILER_1}, Found cxx_compiler at ${CXX_COMPILER_1}")
        set(CMAKE_C_COMPILER "x86_64-linux-gnu-gcc")
        set(CMAKE_CXX_COMPILER "x86_64-linux-gnu-g++")
    elseif (C_COMPILER_2 AND CXX_COMPILER_2)
        message(STATUS "Found c_compiler at ${C_COMPILER_2}, Found cxx_compiler at ${CXX_COMPILER_2}")
        set(CMAKE_C_COMPILER "gcc")
        set(CMAKE_CXX_COMPILER "g++")
    elseif (C_COMPILER_3 AND CXX_COMPILER_3)
        message(STATUS "Found c_compiler at ${C_COMPILER_3}, Found cxx_compiler at ${CXX_COMPILER_3}")
        set(CMAKE_C_COMPILER "cc")
        set(CMAKE_CXX_COMPILER "c++")
    else ()
        message(FATAL_ERROR "compiler not found.")
    endif ()


elseif (TARGET_PLATFORM STREQUAL "")
    message(FATAL_ERROR "Please enter the target platform to build. \n eg : [ win | linux ]")
else ()
    message(FATAL_ERROR "Unsupported platforms, please enter the target platform to build. \n eg : [ win | linux ]")
endif ()

include(${PROJECT_SOURCE_DIR}/cmake/sqlite.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/sqlcipher.cmake)

add_executable(SM4SqliteCipher main.cpp
        ${PROJECT_SOURCE_DIR}/src/storage.cpp
        ${PROJECT_SOURCE_DIR}/src/logging.cpp
)

target_include_directories(SM4SqliteCipher PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_include_directories(SM4SqliteCipher PRIVATE ${PROJECT_SOURCE_DIR}/include/sqlcipher)
target_compile_definitions(SM4SqliteCipher PRIVATE -DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL)
target_link_directories(SM4SqliteCipher PRIVATE ${PROJECT_BINARY_DIR}/openssl_output/lib64)
target_link_libraries(SM4SqliteCipher PRIVATE crypto ssl)
target_link_libraries(SM4SqliteCipher PUBLIC sqlcipher_lib)

if (TARGET_PLATFORM STREQUAL "win")
    target_link_libraries(SM4SqliteCipher PRIVATE crypt32 ws2_32 gdi32)
    target_link_options(SM4SqliteCipher PRIVATE -static)
elseif (TARGET_PLATFORM STREQUAL "linux")
    target_link_libraries(SM4SqliteCipher PRIVATE m dl pthread)
endif ()

add_dependencies(SM4SqliteCipher openssl-install sqlcipher_lib)



